package com.example.diceroller

import android.graphics.Paint.Align
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.Button
import androidx.compose.material3.Divider
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.modifier.modifierLocalConsumer
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.diceroller.ui.theme.DiceRollerTheme
import kotlinx.coroutines.launch
import java.lang.Exception


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DiceRollerTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Drawer()
                }
            }
        }
    }
}

const val firstColor = 0xFFbe98aa
const val secondColor = 0xFF3e3f4c


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DiceRoller(modifier: Modifier = Modifier) {
    var result by remember { mutableStateOf("1") }
    var low by remember { mutableStateOf("1") }
    var up by remember { mutableStateOf("6") }
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = modifier
            .fillMaxSize()
            .background(Color(secondColor))
    ) {
        Spacer(modifier = Modifier.weight(1f))
//        Canvas(modifier = Modifier) {
//            // 绘制填充圆
//            drawCircle(
//                color = Color(firstColor), // 填充颜色
//                center = Offset(size.width / 2f, size.height / 2f), // 圆心坐标
//                radius = 120.dp.toPx(), // 半径大小
//
//            )
//        }
//        Text(
//            text = result,
//            fontSize = 120.sp,
//            textAlign = TextAlign.Center,
//            fontFamily = FontFamily.Monospace,
//            color = Color(secondColor),
//            modifier = Modifier
//                .size(180.dp)
////                .clip(CircleShape)
////                .background(Color(firstColor))
//        )


        Canvas(modifier = Modifier) {
            drawCircle(
                color = Color(firstColor),
                center = Offset(size.width / 2f, size.height / 2f + 220),
                radius = 120.dp.toPx(),
            )
        }

        Column(
            modifier = Modifier
                .align(Alignment.CenterHorizontally)
                .padding(bottom = 18.dp),
            verticalArrangement = Arrangement.Center,
        ) {
            Text(
                text = result,
                fontSize = 110.sp,
                fontFamily = FontFamily.Monospace,
                color = Color(secondColor),
                modifier = Modifier
            )
        }


        Spacer(modifier = Modifier.weight(0.7f))
        Button(
            modifier = Modifier.size(128.dp, 52.dp),
            onClick = {
                result = try {
                    (low.toInt()..up.toInt()).random().toString()
                } catch (e: Exception) {
                    e.printStackTrace()
                    "NaN"
                }
            }
        ) {
            Text(
                text = stringResource(R.string.roll),
                fontSize = 26.sp,
                fontWeight = FontWeight.Bold,
                color = Color(secondColor)
            )
        }
        Spacer(modifier = Modifier.weight(0.5f))
        var expanded by remember { mutableStateOf(false) }
        Column(
            modifier = Modifier.clickable { expanded = !expanded }
        ) {
            Image(
                painter = painterResource(id = R.drawable.dice),
                contentDescription = null,
                modifier = Modifier.align(CenterHorizontally)
            )
            AnimatedVisibility(visible = expanded) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                ) {
                    Spacer(modifier = Modifier.weight(0.5f))
//                    var isFocused by remember { mutableStateOf(false) }
//                    val focusRequester = remember { FocusRequester() }
//                    val focusManager = LocalFocusManager.current
                    OutlinedTextField(
                        value = low,
                        onValueChange = {low = it},
                        label = { Text(
                            text = "low",
                            color = Color.White
                        ) },
                        colors = TextFieldDefaults.outlinedTextFieldColors(
                            textColor = Color.White
                        ),
                        modifier = Modifier
                            .weight(1f)
                    )
                    Spacer(modifier = Modifier.weight(0.2f))
                    OutlinedTextField(
                        value = up,
                        onValueChange = {up = it},
                        label = { Text(
                            text = "up",
                            color = Color.White
                        ) },
                        colors = TextFieldDefaults.outlinedTextFieldColors(
                            textColor = Color.White, // 文本默认颜色
                        ),
                        modifier = Modifier
                            .weight(1f)
                    )
                    Spacer(modifier = Modifier.weight(0.5f))
                }
            }

        }

        Spacer(modifier = Modifier.weight(1f))
    }
}


@OptIn(ExperimentalMaterial3Api::class)
@Preview(
    showSystemUi = true,
    showBackground = true
)
@Composable
fun Drawer() {
    val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
    val scope = rememberCoroutineScope()
    ModalNavigationDrawer(
        drawerState = drawerState,
        // 未知问题，无效背景
//        modifier = Modifier.background(Color(firstColor)),
        drawerContent = {
            ModalDrawerSheet { /* Drawer content */
                Column(
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier
                        .fillMaxWidth(0.7f)
                ) {
                    Column(
                        verticalArrangement = Arrangement.Center,
                        horizontalAlignment = Alignment.CenterHorizontally,
                        modifier = Modifier
                            .fillMaxHeight(0.2f)
                            .fillMaxWidth()
                            .background(Color(firstColor))
                    ) {
                        Row(
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.Center
                        ) {
                            Image(painter = painterResource(id = R.drawable.ic1),
                                contentDescription = null,
                                modifier = Modifier.clip(CircleShape)
                            )
                            Column {
                                Text(
                                    text = "Galangel",
                                    fontSize = 32.sp,
                                    fontWeight = FontWeight.Bold
                                )
                                Text(
                                    text = "QQ: 609027883",
                                    fontSize = 16.sp,
                                    fontWeight = FontWeight.Bold
                                )
                            }
                        }
                    }
                    Spacer(modifier = Modifier.fillMaxHeight(0.1f))
                    Button(onClick = { /*TODO*/ }) {
                        Text(text = "设置")
                    }
                }
            }
        },
    ) {
        Scaffold(
            floatingActionButton = {
                ExtendedFloatingActionButton(
                    text = { Text("DEMO") },
                    icon = { Icon(Icons.Filled.Add, contentDescription = "") },
                    onClick = {
                        scope.launch {
                            drawerState.apply {
                                if (isClosed) open() else close()
                            }
                        }
                    }
                )
            }
        ) {
            // Screen content
            DiceRoller()
            it
        }
    }
}


@Preview(
    showBackground = true,
    showSystemUi = true
)
@Composable
fun DiceRollerPreview() {
    DiceRollerTheme {
        DiceRoller()
    }
}